import java.util.HashMap;
import java.util.Map;

/**
 * leecode 146
 * LRU cache while not using LinkedHashMap
 * @author zzy
 */
public class L146LruCache {
    /**
     * 双端队列中的节点
     */
    class Node<K, V>{

        K key;
        V value;

        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            prev = next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            prev = next = null;
        }
    }

    /**
     * 双端队列
     */
    class DoubleLinkedList<K, V> {

        //两个类似AQS结构的哨兵节点
        Node<K, V> head;
        Node<K, V> tail;

        public DoubleLinkedList() {
            this.head = new Node<>();
            this.tail = new Node<>();

            this.head.next = tail;
            this.tail.prev = head;
        }

        //在头部新增节点
        public void addHead(Node<K, V> node) {

            node.next = head.next;
            node.prev = head;

            this.head.next.prev = node;
            this.head.next = node;

        }

        //删除某一节点
        public void deleteNode(Node<K, V> node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;

            node.next = null;
            node.prev = null;
        }

        //获得最后一个节点
        public Node<K, V> getTail() {
            if(this.tail.prev == this.head){
                return null;
            }
            return tail.prev;
        }
    }

    /**
     * 缓存容量
     */
    private int capacity;

    /**
     * map放具体缓存的值
     */
    HashMap<Integer,Node<Integer, Integer>> map;

    /**
     * 双端队列维护lru约束
     */
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public L146LruCache(int capacity) {
        this.capacity = capacity;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key) {
        if(!map.containsKey(key)) {
            return -1;
        }

        Node<Integer, Integer> node = map.get(key);

        //缓存队列里把最近使用的重新加入
        doubleLinkedList.deleteNode(node);
        doubleLinkedList.addHead(node);

        return node.value;
    }

    public void put(int key, int value) {
        if(map.containsKey(key)){
            //map中已存在key，则更新map值，同时维护lru缓存队列
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);

            doubleLinkedList.deleteNode(node);
            doubleLinkedList.addHead(node);
        }else {
            //map中不存在key，新节点加入，需判断现有容量是否已经满
            if(capacity == map.size()){
                Node<Integer, Integer> tailNode = doubleLinkedList.getTail();
                doubleLinkedList.deleteNode(tailNode);
                map.remove(tailNode.key);

            }
            Node<Integer, Integer> node = new Node<>(key, value);
            map.put(key, node);
            doubleLinkedList.addHead(node);

        }
    }


    public static void main(String[] args) {
        L146LruCache cache = new L146LruCache(3);


        cache.put(1,1);
        cache.put(2,2);
        cache.put(3,3);
        System.out.println(cache.map);
        System.out.println(cache.doubleLinkedList.head.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.next.value);


        cache.put(4,4);
        System.out.println(cache.map);
        System.out.println(cache.map);
        System.out.println(cache.doubleLinkedList.head.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.next.value);

        cache.put(3,3);
        System.out.println(cache.map);
        System.out.println(cache.map);
        System.out.println(cache.doubleLinkedList.head.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.next.value);

        cache.put(3,3);
        cache.put(3,3);
        cache.put(5,5);
        System.out.println(cache.map);
        System.out.println(cache.map);
        System.out.println(cache.doubleLinkedList.head.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.next.value);


        cache.put(6,6);
        System.out.println(cache.map);
        System.out.println(cache.map);
        System.out.println(cache.doubleLinkedList.head.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.value + ">>>" +
                cache.doubleLinkedList.head.next.next.next.value);
    }
}
//
//{1=L146LruCache$Node@60e53b93, 2=L146LruCache$Node@5e2de80c, 3=L146LruCache$Node@1d44bcfa}
//3>>>2>>>1
//{2=L146LruCache$Node@5e2de80c, 3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2}
//{2=L146LruCache$Node@5e2de80c, 3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2}
//4>>>3>>>2
//{2=L146LruCache$Node@5e2de80c, 3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2}
//{2=L146LruCache$Node@5e2de80c, 3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2}
//3>>>4>>>2
//{3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2, 5=L146LruCache$Node@6f94fa3e}
//{3=L146LruCache$Node@1d44bcfa, 4=L146LruCache$Node@266474c2, 5=L146LruCache$Node@6f94fa3e}
//5>>>3>>>4
//{3=L146LruCache$Node@1d44bcfa, 5=L146LruCache$Node@6f94fa3e, 6=L146LruCache$Node@5e481248}
//{3=L146LruCache$Node@1d44bcfa, 5=L146LruCache$Node@6f94fa3e, 6=L146LruCache$Node@5e481248}
//6>>>5>>>3